package com.user.domain;

import com.google.common.base.MoreObjects;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.Clob;
import java.sql.SQLException;

import static java.util.Arrays.copyOf;

/**
 * Created by sola on 2017/9/23.
 */
@Entity
public class User implements Serializable{
    private static final long serialVersionUID = 3202385110553808799L;
    @Id
    private String id;
    @Version
    private Long version;
    @NotBlank(message = "ssssssssssss")
    private String userName;

    private String password;

    private String roles;

    private byte[] description;

    private Clob description2;

    @Transient
    private String abc;

    public User() {
    }

    public User(String id, String userName) {
        this.id = id;
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public String getAbc() {
        return abc;
    }

    public void setAbc(String abc) {
        this.abc = abc;
    }

    public byte[] getDescription() {
        return description == null ? new byte[0] : copyOf(description, description.length);
    }

    public void setDescription(byte[] description) {
        this.description = description == null ? new byte[0] : copyOf(description, description.length);
    }

    public String getDescriptionStr() throws UnsupportedEncodingException {
        return description == null ? "" : new String(description, "UTF-8");
    }

    public String getDescriptionStr2() throws UnsupportedEncodingException {
        return description == null ? "" : new String(description, "GBK");
    }

    public String getDescription2() throws SQLException {
        return (description2 != null ? description2.getSubString(1, (int) description2.length()) : null);
    }


//    public void setDescriptionStr(String descriptionStr) throws UnsupportedEncodingException {
//        this.description = null==descriptionStr ? new byte[0] : descriptionStr.getBytes("UTF-8");
//    }


    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("id", id)
                .add("version", version)
                .add("userName", userName)
                .add("password", password)
                .add("roles", roles)
                .add("description", description)
                .add("description2", description2)
                .add("abc", abc)
                .toString();
    }
}
